<?php
class MyWechat extends Wechat {

    public function  __construct($token, $debug = FALSE)
    {
        parent::__construct($token,$debug);
        //slog($this->getRequest());
        session_id(md5($this->getRequest('fromusername')));
        session_start();
        if(!isset($_SESSION['username']))
        {
            //SELECT * FROM `username` WHERE `weixin_username`='[fromusername]'
            //将用户信息存在session中
        }
    }

    /**
     * 关注微信
     */
    protected function onSubscribe() {
          $this->responseText("欢迎关注JobDeer\n 输入[list]查看人才\n输入人才编号进行抢人才\n输入[bind]绑定账号\n输入[info]查看个人信息\n输入[help]显示帮助");
    }

    /**
     * 收到文本消息时触发，回复收到的文本消息内容
     *
     * @return void
     */
    protected function onText(){
        //输入状态，默认为normal
        $input_status=isset($_SESSION['input_status'])?$_SESSION['input_status']:'normal';
        $content=trim($this->getRequest('content'));
        switch($input_status)
        {
            case "normal":
                if('list'==$content)
                {
                    $this->showList();
                }
                elseif('bind'==$content)
                {
                    //绑定账号 
                    $this->responseText('请点击链接进行绑定账号: http://weixin.i.jobdeer.com/bind.php?fromusername='.$this->getRequest('fromusername'));
                }
                elseif('info'==$content)
                {
                    $this->showInfo();

                }
                elseif('clear'==$content)
                {
                    //开发时用于测试的命令
                    session_destroy(); 
                    $this->responseText('SESSION 已清空');
                }
                elseif(is_numeric($content))
                {
                    //判断接收到的内容，数字为抢人才的编号
                    if(!isset($_SESSION['intro']))
                    {
                        //如果没有个人介绍session，记录要抢人才的编号，并session状态为edit_intro
                        $_SESSION['input_status']='edit_intro';
                        $_SESSION['number']=$content;
                        $this->responseText('您还没有输入个人信息，请先输入个人信息');
                        return ;
                    }
                    $_SESSION['input_status']='input_price';
                    $this->responseText('请输入您的竞拍价格，输入年薪，单位为万元。');
                }
                else
                {
                    $this->responseText("[帮助菜单]\n输入[list]查看人才\n输入人才编号进行抢人才\n输入[bind]绑定账号\n输入[info]查看个人信息\n输入[help]显示帮助");
                }
            break;
            case "edit_intro":
                $_SESSION['intro']=$content;
                //设置input状态为input_price
                $_SESSION['input_status']='input_price';
                $this->responseText('请输入您的竞拍价格，输入年薪，单位为万元。');
            break; 
            case "input_price":
                //设置input状态为normal
                $_SESSION['input_status']='normal';
                //实际应用还会对数据表进行操作。
                $this->responseText('提交成功，您的出价是：年薪'.$content.'万，请耐心等待人才回复');
            break;
            default:
                $this->responseText('输入状态错误');
            break;
        }
    }

    private function showList()
    {
          $items = array(
            new NewsResponseItem('No.58,两年IOS开发经验', '', 'http://weixin.i.jobdeer.com/img/banner.png', 'http://db.jobdeer.com/?c=single&a=rec&id=58'),
            new NewsResponseItem('No.81,三年Android开发经验，大公司背景', '', 'http://weixin.i.jobdeer.com/img/avatar1.png', 'http://db.jobdeer.com/?c=single&a=rec&id=81'),
            new NewsResponseItem('No.93,五年工作经验，有创业经历', '', 'http://weixin.i.jobdeer.com/img/avatar2.png', 'http://db.jobdeer.com/?c=single&a=rec&id=93'),
            new NewsResponseItem('No.104,四年工作经验，大公司背景', '', 'http://weixin.i.jobdeer.com/img/avatar3.png', 'http://db.jobdeer.com/?c=single&a=rec&id=104')
          );
          $this->responseNews($items); 
    }

    private function showInfo()
    {
            //显示个人信息
            if(!isset($_SESSION['username']))
            {
                $fromusername=$this->getRequest('fromusername');
                $this->responseText('您还没有绑定账号，请点击链接进行绑定账号:http://weixin.i.jobdeer.com/bind.php?fromusername='.$fromusername);
                return ;
            }
            $this->responseText('您绑定的账号是：'.$_SESSION['username']);
    }


    /**
     *
    菜单：
{
    "button": [
        {
            "type": "click", 
            "name": "人才列表", 
            "key": "list"
        }, 
        {
            "type": "click", 
            "name": "我的信息", 
            "key": "info"
        }, 
        {
            "type": "view", 
            "name": "关于JobDeer", 
            "url": "http://www.jobdeer.com/"
        }
    ]
}
     */ 

     protected function onClick() 
     {
         $key=$this->getRequest('EventKey');
         if('list'==$key)
         {
             $this->showList(); 
         }
         elseif('info'==$key)
         {
             $this->showInfo(); 
         }
     }


  }


